﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cmsservice.Entity;

namespace cmsservice.DAL
{
    public class NewsDAL
    {
        //StockTPDAL

        public NewEntity GetNewStockTPOne(int newsId)
        {
            NewEntity entity = new NewEntity();
            string sql = "select *from News2 where id=@id";
            Tools tool = new Tools();
            tool.AddSqlParameter("id", System.Data.SqlDbType.Int, newsId);
            return SqlHelper.GetDataByColumns<NewEntity>(DBConn.CMS, sql, tool.GetSqlParameters());

        }

        //新DMS接口中返回详细信息 孙强
        public NewEntity getDMSListDALONE(int newsId)
        {
            NewEntity entity = new NewEntity();
            string sql = " select newsid_num as Id,f006v_12001 as Title,rectime as PTime,f015v_12001 as content,f043v_12001 as keyword,f031v_12001 as Source,code as stock,";
                   sql+= " f013v_12001 as Summary from dbo.vm_dms_getnewsbycode where newsid_num = '" + newsId + "' order by rectime desc";
            Tools tool = new Tools();
            tool.AddSqlParameter("id", System.Data.SqlDbType.Int, newsId);
            return SqlHelper.GetDataByColumns<NewEntity>(DBConn.CMS, sql, tool.GetSqlParameters());

        }
        public List<NewEntity> GetNewStockTP(int top)
        {
            List<NewEntity> lst = new List<NewEntity>();
            string sql = "select top (@top) * from News2 order by ctime desc ";
            Tools tool = new Tools();
            tool.AddSqlParameter("top", System.Data.SqlDbType.Int, top);
            return SqlHelper.GetDataByColumnsLst<NewEntity>(DBConn.CMS, sql, tool.GetSqlParameters());
        }


        public NewEntity GetNewOneBySecuCode(string secuCode)
        {
            NewEntity entity = new NewEntity();
            string sql = "select top 1 * from News2 where stock=@stock order by ctime desc";
            Tools tool = new Tools();
            tool.AddSqlParameter("stock", System.Data.SqlDbType.VarChar, secuCode);
            return SqlHelper.GetDataByColumns<NewEntity>(DBConn.CMS, sql, tool.GetSqlParameters());
        }

        public NewsClickEntity GetNewsClick(int NewId)
        {
            string sql = "select  * from NewsClick where NewsID=@NewsID";
            Tools tool = new Tools();
            tool.AddSqlParameter("NewsID", System.Data.SqlDbType.Int, NewId);
            return SqlHelper.GetDataByColumns<NewsClickEntity>(DBConn.CMS, sql, tool.GetSqlParameters());
        }

        public int UpdateNewsClick(int NewId)
        {
            DateTime dt = DateTime.Now;
            string sql = "update  NewsClick set NewsClickCount=NewsClickCount+1, UpdateDate=@UpdateDate where NewsID=@NewsID";
            Tools tool = new Tools();
            tool.AddSqlParameter("NewsID", System.Data.SqlDbType.Int, NewId);
            tool.AddSqlParameter("UpdateDate", System.Data.SqlDbType.DateTime, dt);
            return SqlHelper.ExecuteNonQuery(DBConn.CMS, System.Data.CommandType.Text,sql, tool.GetSqlParameters());
        }
        public int AddNewsClick(int NewId)
        {
            DateTime dt = DateTime.Now;
            string sql = "insert into  NewsClick(NewsID,NewsClickCount)values(@NewsID,1)";
            Tools tool = new Tools();
            tool.AddSqlParameter("NewsID", System.Data.SqlDbType.Int, NewId);
            return SqlHelper.ExecuteNonQuery(DBConn.CMS, System.Data.CommandType.Text, sql, tool.GetSqlParameters());
        }

    }

}
